package org.apache.rocketmq.example.quickstart.batch;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;

import java.util.ArrayList;
import java.util.List;

/**
 * 批量消息生产者
 */
public class BatchProducer {
    public static void main(String[] args) throws MQClientException {
        DefaultMQProducer producer = new DefaultMQProducer("producer-group-1");
        producer.setNamesrvAddr("39.107.66.31:9876");
        producer.start();

        // 批量发送消息
        try {
            // 如果消息总大小超过4M，需要手动切分
            producer.send(buildBatchMessage());
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            producer.shutdown();
        }
    }

    private static List<Message> buildBatchMessage() {
        List<Message> messageList = new ArrayList<>();
        messageList.add(new Message("message-topic-7", null, "message-1".getBytes()));
        messageList.add(new Message("message-topic-7", null, "message-2".getBytes()));
        messageList.add(new Message("message-topic-7", null, "message-3".getBytes()));
        messageList.add(new Message("message-topic-7", null, "message-4".getBytes()));
        messageList.add(new Message("message-topic-7", null, "message-5".getBytes()));
        messageList.add(new Message("message-topic-7", null, "message-6".getBytes()));
        messageList.add(new Message("message-topic-7", null, "message-7".getBytes()));

        return messageList;
    }
}
